<?php
class ConfigController extends AdminController
{
	public $layout = '/layouts/config';
	
	public $pageHeader;
	
	protected function beforeAction($action) {
		parent::beforeAction($action);
		$this->pageTitle = Yii::app()->name." - Configuration";
		return true;
	}
	
	protected function _renderSidebar() {
		$this->renderPartial('sidebar');
	}
	
	public function actionIndex() {
		$this->pageHeader = 'Web';
		$this->render('index', array(
		));
	}
	
	public function actionExport() {
		$this->pageHeader = 'Export Database';
		if (Yii::app()->request->isPostRequest) {
			$h = new DbHelper();
			$h->exportAndDownload();
		}
		$this->render('export');
	}

	public function actionImport() {
		$this->pageHeader = 'Import Database';
		$model = new ImportForm();
		if (isset($_POST['ImportForm'])) {
			$model->attributes = $_POST['ImportForm'];
			$model->file = CUploadedFile::getInstance($model, 'file');
			
			if ($model->validate()) {
				$file = Yii::getPathOfAlias('webroot').'/upload/' . (string)$model->file;
				$model->file->saveAs($file);
				$h = new DbHelper();
				$h->import($file);

				Yii::app()->user->setFlash('success', "Database imported.");
				$this->redirect('import');
			}
		}
		$this->render('import', array(
			'model'=>$model
		));
	}
}